package com.Servlet;

import com.bean.User;
import com.service.UserService;
import com.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        User user = new User();
        UserServiceImpl userService =new UserServiceImpl();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
//        if (userService.queryUserBySelect(user)!=null){
        if (userService.queryUserBySelect(user)!=null){
            request.getSession().removeAttribute("msg"); //清空作用域
            request.setAttribute("user",user);
            request.getRequestDispatcher("/login_success.jsp").forward(request,response);
        }else{
            request.getSession().setAttribute("msg","用户名或密码错误");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }
}
